using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using MercuryParticleEngine;
using MercuryParticleEngine.Emitters;
using MercuryParticleEngine.Modifiers;


namespace BumbleGame
{
    class PollenBall:AnimatedTexture
    {
        public int pollenState;
        public int color;
        private int transitionFrames, randomCounter, xAdd, yAdd;
        public bool isInPlay, isOnFoot, transitionLeft, transitionRight;
        //private bool isOnFoot, transitionLeft, transitionRight;
        private Random xRand, yRand;

        //constructor
        public PollenBall()
        {
            pollenState = 0;
            isInPlay = false;
            isOnFoot = false;
            transitionRight = false;
            transitionLeft = false;
            transitionFrames = 0;
            color = 0;

            yRand = new Random(0);
            xRand = new Random(1);
            xAdd = 2;
            yAdd = 2;
        
        }
                
        public void Update(float scrollY, float elapsed, Emitter emitterLeft, Emitter emitterRight, Emitter emitterLeftConstant, Emitter emitterRightConstant, Bumblepig bumblepig, List<PollenBall> pollenList)
        {
            UpdateFrame(elapsed);
            if (isInPlay)
            {
                if (!isOnFoot && !transitionLeft && !transitionRight)
                {
                    if (bumblepig.BumbleBody.HitBox2.Intersects(this.HitBox2) && bumblepig.onTopLayer)
                    {
                        isOnFoot = true;
                        if (SpritePosition.X < bumblepig.spritePosition.X)
                        {
                            foreach(PollenBall pollenball in pollenList) 
                            {
                                if (pollenball.pollenState == 1)
                                {
                                    //pollenball.pollenState = 3;
                                    //pollenball.transitionLeft = true;
                                    //pollenball.isOnFoot = false;
                                    this.isOnFoot = false;
                                }
                            }
                            
                            if (isOnFoot)
                            {
                                pollenState = 1;
                                emitterLeft.DischargeQuantity = 5;
                                emitterLeftConstant.DischargeQuantity = 2;
                            }
                        }
                        else
                        {
                            foreach (PollenBall pollenball in pollenList)
                            {
                                if (pollenball.pollenState == 2)
                                {
                                    //pollenball.pollenState = 3;
                                    //pollenball.transitionRight = true;
                                    //pollenball.isOnFoot = false;
                                    this.isOnFoot = false;
                                }
                            }
                            
                            if (isOnFoot)
                            {
                                pollenState = 2;
                                emitterRight.DischargeQuantity = 5;
                                emitterRightConstant.DischargeQuantity = 2;
                            }
                        }
                    }
                }
                switch (pollenState)
                {
                    case 0:
                        SpritePosition.Y += scrollY;
                        break;
                    case 1:
                        SpritePosition = emitterLeft.Position;
                        if (SpriteScale < 0.25f)
                        {
                            emitterLeft.DischargeQuantity = 0;
                            emitterLeftConstant.DischargeQuantity = 0;
                            SpriteScale = 0;
                            pollenState = 3;
                            isInPlay = false;
                            isOnFoot = false;
                        }
                        if (bumblepig.dying)
                        {
                            SpriteScale -= 0.02f;
                        }
                        break;
                    case 2:
                        SpritePosition = emitterRight.Position;
                        if (SpriteScale < 0.25f)
                        {
                            emitterRight.DischargeQuantity = 0;
                            emitterRightConstant.DischargeQuantity = 0;
                            SpriteScale = 0;
                            pollenState = 3;
                            isInPlay = false;
                            isOnFoot = false;
                        }
                        if (bumblepig.dying)
                        {
                            SpriteScale -= 0.02f;
                        }
                        break;
                    case 3: //floating movement will go here
                        if (transitionFrames == 20)
                        {
                            transitionLeft = false;
                            transitionRight = false;
                            transitionFrames = 0;
                        }
                        if (transitionLeft)
                        {
                            SpritePosition.X -= 2;
                            SpritePosition.Y += 2;
                            transitionFrames++;
                        }
                        if (transitionRight)
                        {
                            SpritePosition.X += 2;
                            SpritePosition.Y += 2;
                            transitionFrames++;
                        }
                        if (!transitionLeft && !transitionRight)
                        {
                            if (randomCounter == 15)
                            {
                                yAdd = yRand.Next(1, 4);
                            }
                            if (randomCounter == 30)
                            {
                                yAdd = yRand.Next(1, 4);
                                xAdd = xRand.Next(-2, 3);
                                if (xAdd == 0) xAdd = -1;                                
                                randomCounter = 0;
                            }
                            SpritePosition.Y += yAdd;
                            SpritePosition.X += xAdd;
                            randomCounter++;
                        }
                        //isInPlay = false;
                        break;
                }                

                switch (color)
                {
                    case 0:
                        //FrameRight = 0;
                        tint = Color.Blue;
                        if (pollenState == 1)
                        {
                            emitterLeft.Frame = 1;
                            emitterLeftConstant.Frame = 1;
                        }
                        if (pollenState == 2)
                        {
                            emitterRight.Frame = 1;
                            emitterRightConstant.Frame = 1;
                        }
                        break;
                    case 1:
                        //FrameRight = 1;
                        tint = Color.Red;
                        if (pollenState == 1)
                        {
                            emitterLeft.Frame = 4;
                            emitterLeftConstant.Frame = 4;
                        }
                        if (pollenState == 2)
                        {
                            emitterRight.Frame = 4;
                            emitterRightConstant.Frame = 4;
                        }
                        break;
                    case 2:
                        //FrameRight = 2;
                        tint = Color.Yellow;
                        if (pollenState == 1)
                        {
                            emitterLeft.Frame = 7;
                            emitterLeftConstant.Frame = 7;
                        }
                        if (pollenState == 2)
                        {
                            emitterRight.Frame = 7;
                            emitterRightConstant.Frame = 7;
                        }
                        break;
                }
            }
            if (SpriteScale < 0.15)
            {
                SpriteScale = 0;
                isInPlay = false;
            }
        }

        

    }
}
